//
//  BVMyInvitedViewController.m
//  BuyVegetables
//
//  Created by 叶思超 on 15/10/13.
//  Copyright (c) 2015年 叶思超. All rights reserved.
//

#import "BVMyInvitedViewController.h"
#import "BVConst.h"
#import "UIView+Extension.h"
#import "AppDelegate.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKExtension/SSEShareHelper.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>
#import <ShareSDKUI/SSUIShareActionSheetStyle.h>
#import <ShareSDKUI/SSUIShareActionSheetCustomItem.h>
#import <ShareSDK/ShareSDK+Base.h>
#import "HttpTool.h"
#import "BVLoginViewController.h"

@interface BVMyInvitedViewController ()
/**
 *  邀请活动信息
 */
@property (nonatomic, strong) NSDictionary *activityContent;
@property (nonatomic, strong) NSString *user_id;


@end

@implementation BVMyInvitedViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 导航栏基本设置
    [self setupNav];
    
    // 获取邀请码信息
    [self setupInvitedInfo];
}

#pragma mark -- 获取邀请信息
- (void)setupInvitedInfo {
    // 拼接路径
    NSString *url = [NSString stringWithFormat:@"%@/VegetableUserAPI/getMyInviteInfoById", Path];
    
    // 拼接参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *userinfo = [defaults dictionaryForKey:@"USERINFO"];
    self.user_id = userinfo[@"id"];
    if (self.user_id != nil) {
        params[@"user_id"] = self.user_id;
        
        [HttpTool get:url params:params success:^(id responseObj) {
            if ([responseObj[@"code"] isEqualToString:@"1"]) {
                BVLog(@"用户提交反馈成功 ---%@", responseObj);
                self.activityContent = responseObj[@"data"];
                
                // 设置UI
                [self setupUI];
            } else {
                BVLog(@"用户提交反馈信息失败====%@", responseObj);
            }
        } failure:^(NSError *error) {
            BVLog(@"请求失败======%@", error);
        }];
    } else {
        
    }
}

#pragma mark -- 导航栏基本设置
- (void)setupNav {
    self.navigationController.navigationBar.barTintColor = BVBaseOrangeColor;
    
    self.title = @"我的邀请";
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(backClick)];
}

#pragma mark -- 设置UI
- (void)setupUI {
    // 我的邀请标识
    UIImageView *invitedImage = [[UIImageView alloc] init];
    CGFloat invitedW = 200;
    CGFloat invitedH = 210;
    CGFloat invitedX = BVScreenW * 0.5 - invitedW * 0.5;
    CGFloat invitedY = 84;
    invitedImage.frame = CGRectMake(invitedX, invitedY, invitedW, invitedH);
    invitedImage.image = [UIImage imageNamed:@"wdyq_mail"];
    [self.view addSubview:invitedImage];
    
    UILabel *invitedLabel = [[UILabel alloc] init];
    invitedLabel.frame = CGRectMake(0, 45, invitedImage.width, 20);
    invitedLabel.text = @"我的邀请码";
    invitedLabel.textAlignment = NSTextAlignmentCenter;
    invitedLabel.font = [UIFont systemFontOfSize:15];
    [invitedImage addSubview:invitedLabel];
    
    // 获取我的邀请码
    UILabel *getInvitedLabel = [[UILabel alloc] init];
    getInvitedLabel.frame = CGRectMake(0, CGRectGetMaxY(invitedLabel.frame), invitedImage.width, 20);
    getInvitedLabel.font = [UIFont systemFontOfSize:18];
    getInvitedLabel.textAlignment = NSTextAlignmentCenter;
    getInvitedLabel.text = self.activityContent[@"user_code"];
    getInvitedLabel.textColor = BVBaseOrangeColor;
    [invitedImage addSubview:getInvitedLabel];
    
    // 活动规则内容
    UITextView *textView = [[UITextView alloc] init];
    textView.frame = CGRectMake(10, CGRectGetMaxY(invitedImage.frame) + 10, BVScreenW - 2 * 10, 120);
    textView.font = [UIFont systemFontOfSize:13];
    textView.backgroundColor = [UIColor clearColor];
    textView.textColor = BVBaseTextBlackColor;
    textView.text = self.activityContent[@"acvity_content"];
    [self.view addSubview:textView];
    
    // 分享
    UIButton *shareButton = [[UIButton alloc] init];
    shareButton.frame = CGRectMake(10, CGRectGetMaxY(textView.frame) + 15, BVScreenW - 2 * 10, 40);
    [shareButton setTitle:@"马上分享" forState:UIControlStateNormal];
    [shareButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    shareButton.backgroundColor = BVBaseOrangeColor;
    shareButton.titleLabel.font = [UIFont systemFontOfSize:20];
    shareButton.layer.masksToBounds = YES;
    shareButton.layer.cornerRadius = 4;
    shareButton.layer.borderColor = [UIColor whiteColor].CGColor;
    shareButton.layer.borderWidth = 1;
    [shareButton addTarget:self action:@selector(shareButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:shareButton];
}

#pragma mark -- 分享
- (void)shareButtonClick {
//    1、创建分享参数（必要）
    NSString *urlStr =  [NSString stringWithFormat:@"%@/HouseUserAPI/yqm?userID=%@", Path,self.user_id];
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    NSArray* imageArray = @[[UIImage imageNamed:@"icon.png"]];
    [shareParams SSDKSetupShareParamsByText:@"精菜欢迎您"
                                     images:imageArray
                                        url:[NSURL URLWithString:urlStr]
                                      title:@"分享我的邀请码"
                                       type:SSDKContentTypeAuto];
    
    //设置分享菜单栏样式（非必要）
    [SSUIShareActionSheetStyle setActionSheetColor:BVColor(224, 224, 224)];
    [SSUIShareActionSheetStyle setCancelButtonBackgroundColor:BVColor(224, 224, 224)];
    [SSUIShareActionSheetStyle setCancelButtonLabelColor:BVBaseOrangeColor];
    
    //2、分享
    [ShareSDK showShareActionSheet:self.view
                             items:nil
                       shareParams:shareParams
               onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                   
                   switch (state) {
                       case SSDKResponseStateSuccess:
                       {
                           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                               message:nil
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"确定"
                                                                     otherButtonTitles:nil];
                           [alertView show];
                           break;
                       }
                       case SSDKResponseStateFail:
                       {
                           UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                           message:[NSString stringWithFormat:@"%@",error]
                                                                          delegate:nil
                                                                 cancelButtonTitle:@"OK"
                                                                 otherButtonTitles:nil, nil];
                           [alert show];

                           break;
                       }
        }
    }];
}
#pragma mark -- 返回
- (void)backClick {
    ROOTNAVPOP
}
@end
